﻿/**
 * Created by gzluji on 2016/2/25.
 */
"use strict";
import './style.scss'
class Controller {
    constructor(systemService, uiNotification, $state, $scope, $rootScope, $cookies) {
        "ngInject"
        this.notify            = uiNotification;
        this.service           = systemService;
        this.select            = [];
        this.data              = {}
        ;
        this.state             = $state;
        this.scope             = $scope;
        this.cookies           = $cookies;
        this.rootScope         = $rootScope;
        this.rootScope.isLogin = false;
        this.ctgUser           = $cookies.get('ctgUser') && angular.fromJson($cookies.get('ctgUser')); //|| JSON.parse("{\"staffId\":1,\"sysRoleId\":[],\"username\":\"blackfox\",\"sysUserId\":16}");
        if ($cookies.get('remember')) {
            this.remember         = true;
            this.data.username = $cookies.get('remember');
        }
    }

    toLogin(params) {
        this.remember ? this.cookies.put('remember', this.data.username) : this.cookies.remove('remember')
        this.service
            .login(params)
            .then((r) => {
                console.log(r)
                if (r.data) {
                    this.rootScope.postList = r.data;
                    this.state.go('post');
                } else {
                    this.state.go('corp');
                }
            }).catch((res) => {
            this.notify.error({
                'title'   : '提示',
                'message' : '操作失败. ' + (res.resultMsg ? res.resultMsg : '')
            });
        }).finally(() => {
            this.eLoading = false;
        })
    }

    next($event) {
        if ($event && $event.keyCode !== 13) return;
        this.eLoading = true;
        return this.toLogin(this.data);
    }

    checkValid(field) {
        if (field.$pristine) {
            field.$pristine = false;
        }
    }
}

export default Controller;